package com.struts2.action;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import com.opensymphony.xwork2.ActionSupport;


public class CutPhotoAction extends ActionSupport implements ServletRequestAware, SessionAware{

	private static final long serialVersionUID = 8234888222L;

	private String fileName;
	private String name;
	private String imgChar;

	private int cutX;
	private int cutY;
	private int cutWidth;
	private int cutHeight;
	
	private HttpServletRequest servletRequest;
	
	private Map<String, Object> session;
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@SuppressWarnings("deprecation")
	@Override
	public String execute() throws Exception {
		System.out.println("cut photo action");
		System.out.println(fileName);
		System.out.println(session.get("pid"));
		System.out.println(ServletActionContext.getRequest().getRealPath("/"));
		String destImageFile = ServletActionContext.getRequest().getRealPath("/img/product_line_id");
		setName(session.get("pid") + imgChar + getExtention(fileName));
		String srcImageFile = ServletActionContext.getRequest().getRealPath(fileName);
		Image img;
        ImageFilter cropFilter;
        
        BufferedImage bi = ImageIO.read(new File(srcImageFile));
        int srcWidth = bi.getWidth();
        int srcHeight = bi.getHeight();
        
        if (srcWidth >= cutWidth && srcHeight >= cutHeight) {
            Image image = bi.getScaledInstance(srcWidth, srcHeight,
                    Image.SCALE_DEFAULT);
            cropFilter = new CropImageFilter(cutX, cutY, cutWidth, cutHeight);
            img = Toolkit.getDefaultToolkit().createImage(
                    new FilteredImageSource(image.getSource(), cropFilter));
            BufferedImage tag = new BufferedImage(cutWidth, cutHeight,
                    BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            g.drawImage(img, 0, 0, null);
            g.dispose();
            ImageIO.write(tag, "JPEG", new File(destImageFile, name));
        }
        destImageFile = destImageFile + "/" + name;
        String workspace = "C:/Users/liklik/workspace/BaseProject/web/img/product_line_id/"+ name;
		FileUtils.copyFile(new File(destImageFile), new File(workspace));
		FileUtils.deleteQuietly(new File(srcImageFile));
		if(imgChar.equals("A")){
			return "doneA";
		}else if(imgChar.equals("B")){
			return "doneB";
		}else if(imgChar.equals("C")){
			return "doneC";
		}else {
			session.remove("pid");
			return "success";
		}
	}
	
	private static String getExtention(String fileName) {
		int pos = fileName.lastIndexOf(".");
		return fileName.substring(pos);
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public int getCutX() {
		return cutX;
	}

	public void setCutX(int cutX) {
		this.cutX = cutX;
	}

	public int getCutY() {
		return cutY;
	}

	public void setCutY(int cutY) {
		this.cutY = cutY;
	}

	public int getCutWidth() {
		return cutWidth;
	}

	public void setCutWidth(int cutWidth) {
		this.cutWidth = cutWidth;
	}

	public int getCutHeight() {
		return cutHeight;
	}

	public void setCutHeight(int cutHeight) {
		this.cutHeight = cutHeight;
	}
	
	@Override
	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getImgChar() {
		return imgChar;
	}

	public void setImgChar(String imgChar) {
		this.imgChar = imgChar;
	}

}
